//
//  HomeClassifyCollectionReusableView.swift
//  LeTu
//
//  Created by g1 on 2024/1/11.
//

import UIKit

class HomeClassifyCollectionReusableView: UICollectionReusableView {
            
    var classifys = [ScenicClassifyItem]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    static func getReusableViewHeight() -> CGFloat {
        return 60
    }
    func setClassifys(_ classifys: [ScenicClassifyItem]) {
        self.classifys = classifys
        dataSource.titles = classifys.map({ $0.classifyType.rawValue.localized })
        couponsSegmentedView.reloadData()
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(couponsSegmentedView)
        
        couponsSegmentedView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.width.equalTo(screenWidth-32)
            make.centerY.equalToSuperview()
            make.height.equalTo(44)
        }
        
    }
    private lazy var sliderView: JXSegmentedIndicatorGradientLineView = {//标题背景
        let sliderView = JXSegmentedIndicatorGradientLineView()
        sliderView.colors =  [UIColor(red: 1, green: 0.839, blue: 0, alpha: 1), UIColor(red: 0.851, green: 0.851, blue: 0.851, alpha: 0)]
        return sliderView
    }()
    lazy var dataSource: JXSegmentedTitleDataSource = {
        let ds = JXSegmentedTitleDataSource()
        ds.titles = []
        ds.titleNormalColor =  .app_textGrayColor
        ds.titleSelectedColor = .app_black
        ds.titleNormalFont = kSetPingFangMedium(16)
        ds.titleSelectedFont = kSetPingFangMedium(16)
        ds.itemSpacing = 32
        ds.isItemSpacingAverageEnabled = false
        return ds
    }()
    private(set) lazy var couponsSegmentedView: JXSegmentedView = {
        let sv = JXSegmentedView(frame: CGRect.zero)
        sv.backgroundColor = .app_white
        sv.dataSource = dataSource //数据源
        sv.indicators = [sliderView] //标题下的横线
        sv.contentEdgeInsetLeft = 0
        sv.contentEdgeInsetRight = 0
        sv.delegate = self //设置代理
        return sv
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension HomeClassifyCollectionReusableView: JXSegmentedViewDelegate {
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        send(event: HomeVC.TransferEvent.clickClassify(index))
    }
    
}

